package com.useeinfo.oa.modules.task.dao;

import com.useeinfo.framework.extend.dao.CrudDao;
import com.useeinfo.framework.sugar.data.QueryParam;
import com.useeinfo.framework.sugar.data.QueryUtils;
import com.useeinfo.framework.sugar.tools.StringConverters;
import com.useeinfo.oa.modules.task.entity.TaskOrderLog;
import com.useeinfo.oa.modules.task.enums.TaskOrderLogOperationEnum;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @auther: bin.liu
 * @Date: 2019/6/17 11:19
 * @Description:
 */
@Repository("taskOrderLogDao")
public class TaskOrderLogDao implements CrudDao<TaskOrderLog> {

    private final static Logger logger = LoggerFactory.getLogger(TaskOrderLogDao.class);

    @PersistenceContext
    private EntityManager entityManager;

    @Override
    public Map<String, Object> getSearchCondition(Map<String, String> queryHash){
        Map<String, Object> conditionHash = new HashMap<>();

        conditionHash.put("tol.deleteStatus = ?{paramIndex} ", false);
        if (queryHash == null || queryHash.size() == 0) {
            return conditionHash;
        }

        String taskOrderId = queryHash.get("taskOrderId");
        if (StringUtils.isNotBlank(taskOrderId)) {
            conditionHash.put("tol.taskOrder.id = ?{paramIndex} ", StringConverters.null2Long(taskOrderId));
        }

        String enterpriseId = queryHash.get("enterpriseId");
        if(StringUtils.isNotBlank(enterpriseId)){
            conditionHash.put("tol.enterprise.id = ?{paramIndex} ",StringConverters.null2Long(enterpriseId));
        }

        String progress = queryHash.get("progress");
        if (!StringUtils.isEmpty(progress)) {
            conditionHash.put("tol.taskOrderLogOperationEnum in "+progress+" and 1= ?{paramIndex}", 1);
        }

        String taskOrderLogOperationEnum = queryHash.get("taskOrderLogOperationEnum");
        if(StringUtils.isNotBlank(taskOrderLogOperationEnum)){
            conditionHash.put("tol.taskOrderLogOperationEnum = ?{paramIndex} ", TaskOrderLogOperationEnum.valueOf(taskOrderLogOperationEnum));
        }

        String taskOrderLogOperation = queryHash.get("taskOrderLogOperation");
        if (StringUtils.isNotBlank(taskOrderLogOperation)) {
            String[] statusArray = taskOrderLogOperation.split(",");
            List<String> codes = Arrays.asList(statusArray);
            List<TaskOrderLogOperationEnum> enums = codes.stream().map(s -> TaskOrderLogOperationEnum.valueOf(s)).collect(Collectors.toList());
            conditionHash.put("tol.taskOrderLogOperationEnum in ( ?{paramIndex} ) ", enums);
        }

        return conditionHash;
    }

    /**
     * 获取总记录数
     */
    @Override
    public Long totalRecord(Map<String, String> queryHash){
        Map<String, Object> conditions = getSearchCondition(queryHash);
        TypedQuery<Long> typedQuery = QueryUtils.getTypedQueryByCondition("select count(tol) from TaskOrderLog tol ", conditions, entityManager, Long.class);
        return typedQuery.getSingleResult();
    }

    /**
     * 分页列表
     */
    @Override
    public List<TaskOrderLog> findList(QueryParam queryParam){
        String sqlInfo = queryParam.joinJPQL("select tol from TaskOrderLog tol ", "order by tol.id desc ");
        Map<String, Object> conditions = getSearchCondition(queryParam.getSqlMap());
        TypedQuery<TaskOrderLog> typedQuery = QueryUtils.getTypedQueryByCondition(sqlInfo, conditions, entityManager, TaskOrderLog.class);
        return queryParam.findPageList(typedQuery);
    }

    /**
     * id获取记录
     */
    @Override
    public TaskOrderLog findModel(Long id){
        return entityManager.find(TaskOrderLog.class, id);
    }

    /**
     * 增加记录
     */
    @Override
    public Integer add(TaskOrderLog TaskOrderLog){
        TaskOrderLog.setCreateDate(new Date());
        entityManager.persist(TaskOrderLog);
        logger.info("DataCatDaoImpl添加dataCat成功！");
        return 1;
    }

    /**
     * 修改记录
     */
    @Override
    public Integer update(TaskOrderLog TaskOrderLog){
        TaskOrderLog.setUpdateDate(new Date());
        entityManager.merge(TaskOrderLog);
        return 1;
    }

    /**
     * 删除记录
     */
    @Override
    public Integer delete(Long id){
        TaskOrderLog TaskOrderLog = entityManager.find(TaskOrderLog.class, id);
        entityManager.remove(TaskOrderLog);
        return 1;
    }

    /**
     * 删除记录 - 逻辑删除
     */
    @Override
    public Integer deleteLogic(TaskOrderLog TaskOrderLog){
        TaskOrderLog.setDeleteStatus(true);
        return 1;
    }
}
